package com.andyage.admin.service.impl;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.andyage.admin.dao.AdfodderdirectoryMapper;
import com.andyage.admin.service.IAdfodderdirectoryService;
import com.andyage.util.GetUUID;
import com.andyage.util.JsonContent;
import com.andyage.admin.bean.AdfodderdirectoryBean;


import net.sf.json.JSONObject;

@Service
public class AdfodderdirectoryServiceImpl implements IAdfodderdirectoryService {
	public String CONTENT_TYPE = "text/html;charset=utf-8";
	@Resource
	private AdfodderdirectoryMapper adfodderdiretoryMapper;

	@Override
	public void queryAdfodderdirectory(AdfodderdirectoryBean adfodder, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<AdfodderdirectoryBean> list = adfodderdiretoryMapper.queryAdfodderdirectory(adfodder);
		int total = adfodderdiretoryMapper.queryAdfodderdirectoryByCount(adfodder);
		String json = JsonContent.getJsonArray(list, total);
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void addAdfodderdirectory(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String fdName=request.getParameter("fdName");
		String fdId=GetUUID.get();
		AdfodderdirectoryBean adfodder=new AdfodderdirectoryBean();
		adfodder.setFdId(fdId);
		adfodder.setFdName(fdName);
		String path=request.getSession().getServletContext().getRealPath("/uploads/"+fdId);
		File file=new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		int flag = adfodderdiretoryMapper.addAdfodderdirectory(adfodder);
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "新建成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "新建失败");
		}

		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void deleteAdfodderdirectory(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fdId = request.getParameter("fdId");
		int flag = adfodderdiretoryMapper.deleteAdfodderdirectory(fdId);
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "删除成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "删除失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateAdfodderdirectory(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fdName=request.getParameter("fdName");
		String fdId=request.getParameter("fdId");
		AdfodderdirectoryBean adfodder=new AdfodderdirectoryBean();
		adfodder.setFdId(fdId);
		adfodder.setFdName(fdName);
		int flag=adfodderdiretoryMapper.updateAdfodderdirectory(adfodder);
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

}
